package net.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class ArrayUtil
{
	private ArrayUtil()
	{
		
	}

	/**
	 * Copies <code>data</code> into target starting at <code>i0</code> and extending to <code>i1</code>.
	 * */
	public static void fill(int[] target, int[] data, int i0, int i1)
	{
		if (i0 < 0 || i1 < 0 || i1 < i0)
		{
			throw new IllegalArgumentException();
		}
		
		for(int n = i0; n < i1; n++)
		{
			target[n] = data[n - i0];
		}
	}
	
	public static String toString(Object[][] a)
	{
		String str = "";
		
		for(Object[] b : a)
		{
			str += Arrays.toString(b);
		}
		
		return str;
	}

	public static int height(Object[][] a)
	{
		int max = 0;
		
		for(Object[] obs : a)
		{
			if (obs.length > max)
			{
				max = obs.length;
			}
		}
		
		return max;
	}
	
	public static int height(int[][] a)
	{
		int max = 0;
		
		for(int[] ints : a)
		{
			if (ints.length > max)
			{
				max = ints.length;
			}
		}
		
		return max;
	}
	
	/**
	 * Returns a copy of the 2d array which is guaranteed to have dimensions <b>at least</b> as large as specified by 
	 * <code>w</code> and <code>h</code>. If the array is already as large or larger than the specified dimensions, it is 
	 * returned unchanged.
	 * 
	 * @param w The "width" of the 2d array (the length of the array).
	 * @param h The "height" of the 2d array (the length of the arrays contained within the primary array).
	 * */
	public static int[][] ensureDimensions(int[][] data, int w, int h)
	{
		if (data.length > w || height(data) > h)
		{
			System.out.println("okay");
			return data;
		}
		
		data = Arrays.copyOf(data, w);
		
		for(int n = 0; n < w; n++)
		{
			data[n] = data[n] == null ? new int[h] : Arrays.copyOf(data[n], h);
		}
		
		return data;
	}
	
	@SafeVarargs
	public static <T> List<T> asList(T[]... a)
	{
		ArrayList<T> result = new ArrayList<>(a.length * a[0].length);
		
		for(T[] t : a)
		{
			result.addAll(Arrays.asList(t));
		}
		
		return result;
	}
	
	public static void fill2d(int[][] a,int val)
	{
		for(int [] a2 : a)
		{
			Arrays.fill(a2, val);
		}
	}
	
	public static String toString(float[][] a)
	{
		String str = "";
		
		for(float[] b : a)
		{
			str += Arrays.toString(b);
		}
		
		return str;
	}

	public static String toGriddedString(int[][] a)
	{
		String str = "";
		
		for(int y = 0; y < a[0].length; y ++)
		{
			for(int x = 0; x < a.length; x ++)
			{
				str += String.valueOf(a[x][y]);
			}
			str += '\n';
		}
		
		return str;
	}
	
	public static String toString(int[][] a)
	{
		String str = "";
		
		for(int[] b : a)
		{
			str += Arrays.toString(b);
		}
		
		return str;
	}
}
